$("#rg_hi").live("mouseover", function(e)
{
    var img = e.target;
    var lnk = img.parentNode;
    var imgUrl = extractImageUrl(lnk.href);
    if (imgUrl != null)
    {
        if (imgUrl.toLowerCase().indexOf(".png") != -1 || imgUrl.toLowerCase().indexOf(".gif") != -1)
        {
            if (lnk.style.backgroundImage != "" && lnk.style.backgroundImage != "none")
            {
                return;
            }
            
            var $img = $(img);
            var $lnk = $(lnk);
            
            var $hint = $("<span class='loadingHint' style='position:absolute;top:0;color:white;background-color:gray;opacity:0.7;font-size:6px;font-weight:bold;padding:2px;padding-left:4px;padding-right:4px;border-radius:3px;'>Loading...</span>");
            $lnk.append($hint);
            
            $img.load(function()
            {
                $hint.remove();
                $lnk.css("backgroundImage", "url(" + chrome.extension.getURL("images/bg_32.jpg") + ")");
            });
            
            setTimeout(function()
            {
                $img.attr("src", imgUrl);                
                $lnk.css("backgroundImage", "url(" + chrome.extension.getURL("images/bg_32.jpg") + ")");
            }, 100);
        }
    }
});



function extractImageUrl(p_url)
{
    var regex = /[\?&]{1}imgurl=([^&]*)/i;
    var result = p_url.match(regex);
    if (result != null && result.length == 2)
    {
        return decodeUrl(result[1]);
    }
    return null;
}

function decodeUrl(p_url)
{
    var regex = /%25([0-9A-D]{2})/g;
    var result = p_url.match(regex);
    if (result != null && result.length >= 2)
    {
        for (var i = 0; i < result.length; i++)
        {
            var chr = String.fromCharCode(parseInt(result[i].substr(3), 16));
            p_url = p_url.replace(result[i], chr);
        }
    }
    return p_url;
}